Migrate from liburl to rust-url
authorAlex Crichton <alex@alexcrichton.com>
Wed, 30 Jul 2014 16:52:37 +0000 (09:52 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 30 Jul 2014 16:55:48 +0000 (09:55 -0700)
The standard url library is soon-to-be deprecated, and now that we're
bootstrapping it's trivial to move over to rust-url.

Closes #204

Cargo.toml
src/bin/cargo-git-checkout.rs
src/cargo/core/package_id.rs
src/cargo/core/resolver.rs
src/cargo/core/source.rs
src/cargo/sources/git/source.rs
src/cargo/util/mod.rs
src/cargo/util/to_url.rs [new file with mode: 0644]
tests/test_cargo_compile_git_deps.rs

index a07eacab502dd1473b0ffad8a970828993f57a32..3d8deef36769a8e3a02d603e5ab52ed7a792c8ad 100644 (file)
@@ -27,6 +27,10 @@ rev = "a3c7f2c3"
 git = "https://github.com/carllerche/hamcrest-rust.git"
 rev = "05acf768"
 
+[dependencies.url]
+git = "https://github.com/servo/rust-url"
+rev = "98a28e85"
+
 [[bin]]
 name = "cargo"
 test = false
index 45427234be6947f01b22e64c38ff8c923dc47ea5..1fbeae79d8c7dc05a19b080c2b96487375a5662f 100644 (file)
@@ -12,7 +12,7 @@ use cargo::{execute_main_without_stdin};
 use cargo::core::MultiShell;
 use cargo::core::source::{Source, SourceId};
 use cargo::sources::git::{GitSource};
-use cargo::util::{Config, CliResult, CliError, Require, human};
+use cargo::util::{Config, CliResult, CliError, human};
 use url::Url;
 
 docopt!(Options, "
@@ -31,9 +31,10 @@ fn main() {
 fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
     let Options { flag_url: url, flag_reference: reference, .. } = options;
 
-    let url: Url = try!(from_str(url.as_slice())
-                        .require(|| human(format!("The URL `{}` you passed was \
-                                                   not a valid URL", url)))
+    let url: Url = try!(Url::parse(url.as_slice()).map_err(|e| {
+                            human(format!("The URL `{}` you passed was \
+                                           not a valid URL: {}", url, e))
+                        })
                         .map_err(|e| CliError::from_boxed(e, 1)));
 
     let source_id = SourceId::for_git(&url, reference.as_slice());
index 02bf6fc2906f94d8a14fda4afc820074013e5ce0..a325b3260244a0cc3ab9bca48136ccb3e260cf22 100644 (file)
@@ -1,5 +1,4 @@
 use semver;
-use url::Url;
 use std::hash::Hash;
 use std::fmt;
 use std::fmt::{Show,Formatter};
@@ -33,28 +32,6 @@ impl<'a> ToVersion for &'a str {
     }
 }
 
-trait ToUrl {
-    fn to_url(self) -> Result<Url, String>;
-}
-
-impl<'a> ToUrl for &'a str {
-    fn to_url(self) -> Result<Url, String> {
-        Url::parse(self)
-    }
-}
-
-impl ToUrl for Url {
-    fn to_url(self) -> Result<Url, String> {
-        Ok(self)
-    }
-}
-
-impl<'a> ToUrl for &'a Url {
-    fn to_url(self) -> Result<Url, String> {
-        Ok(self.clone())
-    }
-}
-
 #[deriving(Clone, PartialEq)]
 pub struct PackageId {
     name: String,
index e5a6dfab1d10e189cc6d9514c63c67baf22ad172..804baef79b2679665cc129d2578ae99998d6ed38 100644 (file)
@@ -135,7 +135,7 @@ mod test {
 
     use core::source::{SourceId, RegistryKind, GitKind, Location, Remote};
     use core::{Dependency, PackageId, Summary, Registry};
-    use util::CargoResult;
+    use util::{CargoResult, ToUrl};
 
     fn resolve<R: Registry>(pkg: &PackageId, deps: &[Dependency], registry: &mut R)
                             -> CargoResult<Vec<PackageId>> {
@@ -148,7 +148,7 @@ mod test {
 
     impl ToDep for &'static str {
         fn to_dep(self) -> Dependency {
-            let url = from_str("http://example.com").unwrap();
+            let url = "http://example.com".to_url().unwrap();
             let source_id = SourceId::new(RegistryKind, Remote(url));
             Dependency::parse(self, Some("1.0.0"), &source_id).unwrap()
         }
@@ -200,13 +200,13 @@ mod test {
     }
 
     fn dep(name: &str) -> Dependency {
-        let url = from_str("http://example.com").unwrap();
+        let url = "http://example.com".to_url().unwrap();
         let source_id = SourceId::new(RegistryKind, Remote(url));
         Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
     }
 
     fn dep_loc(name: &str, location: &str) -> Dependency {
-        let url = from_str(location).unwrap();
+        let url = location.to_url().unwrap();
         let source_id = SourceId::new(GitKind("master".to_string()), Remote(url));
         Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
     }
index 844f4bfcc22c1ebade260fd8859ce3a7842957ed..d98aaab71e7bd495392e37ca34d509574cd3be7c 100644 (file)
@@ -330,13 +330,14 @@ impl Source for SourceSet {
 #[cfg(test)]
 mod tests {
     use super::{SourceId, Remote, GitKind};
+    use util::ToUrl;
 
     #[test]
     fn github_sources_equal() {
-        let loc = Remote(from_str("https://github.com/foo/bar").unwrap());
+        let loc = Remote("https://github.com/foo/bar".to_url().unwrap());
         let s1 = SourceId::new(GitKind("master".to_string()), loc);
 
-        let loc = Remote(from_str("git://github.com/foo/bar").unwrap());
+        let loc = Remote("git://github.com/foo/bar".to_url().unwrap());
         let mut s2 = SourceId::new(GitKind("master".to_string()), loc);
 
         assert_eq!(s1, s2);
index 08d41e6146be444ac60134402eb2985933b747c8..6bd1622e615e7502173c115b486f713dc90187db 100644 (file)
@@ -68,7 +68,8 @@ fn ident(location: &Location) -> String {
             str::from_utf8(last).unwrap().to_string()
         }
         Remote(ref url) => {
-            let path = canonicalize_url(url.path.path.as_slice());
+            let path = url.path().unwrap().connect("/");
+            let path = canonicalize_url(path.as_slice());
             path.as_slice().split('/').last().unwrap().to_string()
         }
     };
@@ -184,6 +185,7 @@ mod test {
     use url::Url;
     use core::source::Remote;
     use super::ident;
+    use util::ToUrl;
 
     #[test]
     pub fn test_url_to_path_ident_with_path() {
@@ -226,6 +228,6 @@ mod test {
     }
 
     fn url(s: &str) -> Url {
-        from_str(s).unwrap()
+        s.to_url().unwrap()
     }
 }
index 017b23f2db0a0695fd35f2c005411ca656935633..7d567e68e87863f0ccd6c5266e74ac48c0b24649 100644 (file)
@@ -9,6 +9,7 @@ pub use self::hex::{to_hex, short_hash};
 pub use self::pool::TaskPool;
 pub use self::dependency_queue::{DependencyQueue, Fresh, Dirty, Freshness};
 pub use self::graph::Graph;
+pub use self::to_url::ToUrl;
 
 pub mod graph;
 pub mod process_builder;
@@ -21,3 +22,4 @@ pub mod errors;
 pub mod hex;
 mod pool;
 mod dependency_queue;
+mod to_url;
diff --git a/src/cargo/util/to_url.rs b/src/cargo/util/to_url.rs
new file mode 100644 (file)
index 0000000..b4baf9e
--- /dev/null
@@ -0,0 +1,34 @@
+use url;
+use url::{Url, UrlParser};
+
+pub trait ToUrl {
+    fn to_url(self) -> Result<Url, String>;
+}
+
+impl ToUrl for Url {
+    fn to_url(self) -> Result<Url, String> {
+        Ok(self)
+    }
+}
+
+impl<'a> ToUrl for &'a Url {
+    fn to_url(self) -> Result<Url, String> {
+        Ok(self.clone())
+    }
+}
+
+impl<'a> ToUrl for &'a str {
+    fn to_url(self) -> Result<Url, String> {
+        UrlParser::new().scheme_type_mapper(mapper).parse(self).map_err(|s| {
+            s.to_string()
+        })
+    }
+}
+
+fn mapper(s: &str) -> url::SchemeType {
+    match s {
+        "git" => url::RelativeScheme("9418"),
+        "ssh" => url::RelativeScheme("22"),
+        s => url::whatwg_scheme_type_mapper(s),
+    }
+}
index a295e85bc4593ef8e10267b169cfcb1e3642e706..9a60115a86f174b1ad1c74743ec7e24c008e631d 100644 (file)
@@ -456,7 +456,7 @@ test!(cargo_compile_with_short_ssh_git {
         execs()
         .with_stdout("")
         .with_stderr(format!("Cargo.toml is not a valid manifest\n\n\
-                              invalid url `{}`: `url: Invalid character in scheme.\n", url)));
+                              invalid url `{}`: `Relative URL without a base\n", url)));
 })
 
 test!(two_revs_same_deps {